package com.ruoyi.attendance.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.attendance.mapper.AttendancePoliciesMapper;
import com.ruoyi.attendance.domain.AttendancePolicies;
import com.ruoyi.attendance.service.IAttendancePoliciesService;

/**
 * 考勤政策/排班详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Service
public class AttendancePoliciesServiceImpl implements IAttendancePoliciesService 
{
    private static final Logger log = LoggerFactory.getLogger(AttendancePoliciesServiceImpl.class);
    @Autowired
    private AttendancePoliciesMapper attendancePoliciesMapper;

    /**
     * 查询考勤政策/排班详情
     * 
     * @param id 考勤政策/排班详情主键
     * @return 考勤政策/排班详情
     */
    @Override
    public AttendancePolicies selectAttendancePoliciesById(Long id)
    {
        return attendancePoliciesMapper.selectAttendancePoliciesById(id);
    }

    /**
     * 查询考勤政策/排班详情列表
     * 
     * @param attendancePolicies 考勤政策/排班详情
     * @return 考勤政策/排班详情
     */
    @Override
    public List<AttendancePolicies> selectAttendancePoliciesList(AttendancePolicies attendancePolicies)
    {
        return attendancePoliciesMapper.selectAttendancePoliciesList(attendancePolicies);
    }

    /**
     * 新增考勤政策/排班详情
     * 
     * @param attendancePolicies 考勤政策/排班详情
     * @return 结果
     */
    @Override
    public int insertAttendancePolicies(AttendancePolicies attendancePolicies)
    {
        // 在插入前检查是否存在相同的记录
        AttendancePolicies policiesByName = attendancePoliciesMapper.selectAttendancePoliciesByName(attendancePolicies.getPolicyName());
        if (policiesByName != null) {
            throw new RuntimeException("考勤政策/排班详情名称已存在");
        }

        // 处理字段数据
        // 描述信息
        String description = attendancePolicies.getDescription();
        if (description != null && description.length() > 200) {
            throw new RuntimeException("描述信息长度不能超过200个字符");
        }else if (description == null) {
            // 给一个默认值
            attendancePolicies.setDescription("暂无描述信息");
        }

        // 打卡容忍时间
        Long toleranceTime = attendancePolicies.getTolerateTimes();
        if (toleranceTime == null || toleranceTime < 0) {
            // 重置为5
            attendancePolicies.setTolerateTimes(5L);
        }

        // 加班判断
        Long overtimeJudge = attendancePolicies.getOvertimeStart();

        if (overtimeJudge == null || overtimeJudge < 0) {
            // 重置为15
            attendancePolicies.setOvertimeStart(15L);
        }

        // 设置创建时间和更新时间
        // 需要的是Date, 用于数据库存储, 用sql
        attendancePolicies.setCreateTime(new Date());
        attendancePolicies.setUpdateTime(new Date());



        return attendancePoliciesMapper.insertAttendancePolicies(attendancePolicies);
    }

    /**
     * 修改考勤政策/排班详情
     * 
     * @param attendancePolicies 考勤政策/排班详情
     * @return 结果
     */
    @Override
    public int updateAttendancePolicies(AttendancePolicies attendancePolicies)
    {
        // 在插入前检查是否存在相同的记录
        AttendancePolicies policiesByName = attendancePoliciesMapper.selectAttendancePoliciesByName (attendancePolicies.getPolicyName());
        if (policiesByName!= null) {
            throw new RuntimeException("考勤政策/排班详情名称已存在");
        }


        return attendancePoliciesMapper.updateAttendancePolicies(attendancePolicies);
    }

    /**
     * 批量删除考勤政策/排班详情
     * 
     * @param ids 需要删除的考勤政策/排班详情主键
     * @return 结果
     */
    @Override
    public int deleteAttendancePoliciesByIds(Long[] ids)
    {
        return attendancePoliciesMapper.deleteAttendancePoliciesByIds(ids);
    }

    /**
     * 删除考勤政策/排班详情信息
     * 
     * @param id 考勤政策/排班详情主键
     * @return 结果
     */
    @Override
    public int deleteAttendancePoliciesById(Long id)
    {
        return attendancePoliciesMapper.deleteAttendancePoliciesById(id);
    }
}
